package com.example.hellonotes.activity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.example.hellonotes.NotesDB;
import com.example.hellonotes.R;
import com.example.hellonotes.R.id;
import com.example.hellonotes.R.layout;
import com.example.hellonotes.adapter.WriteStatusGridImgsAdapter;
import com.example.hellonotes.entity.Person2;
import com.example.hellonotes.utils.GetThumbnail;
import com.example.hellonotes.utils.ImageUtils;
import com.example.hellonotes.utils.TitleBuilder;
import com.example.hellonotes.widget.WrapHeightGridView;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

/**
 * 详情界面
 * @author Administrator
 *
 */

public class selectActivity extends Activity implements OnClickListener,OnItemClickListener{
	
	private TextView timeTV;
	private EditText s_EditText;
	private NotesDB notesDB;
	private SQLiteDatabase dbWriter;
	private int id;
	private String path;
	private String content;
	private String time;
	//private String uris;
	private ArrayList<Uri> imgUriss = new ArrayList<Uri>();
	//private String[] uri;
	//private List<String> list;
	private com.example.hellonotes.adapter.WriteStatusGridImgsAdapter statusImgsAdapter;
	// 添加的九宫格图片
	private WrapHeightGridView gv_write_status;
	private Person2 entity;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select);
		
		initView();
		initPath();
		initTitle();
		
	}
	
	/**
	 * 初始化控件 并获取传递过来的数据
	 */
	public void initView() {
		
		gv_write_status = (WrapHeightGridView) findViewById(R.id.gv_write_status);
		//statusImgsAdapter = new SelectStatusGridImgAdapter(this,imgUriss, gv_write_status);
		statusImgsAdapter = new WriteStatusGridImgsAdapter(this, imgUriss, gv_write_status);
		gv_write_status.setAdapter(statusImgsAdapter);
		gv_write_status.setOnItemClickListener(this);
		id = getIntent().getIntExtra(NotesDB.ID, 0);
	/*	content = getIntent().getStringExtra(NotesDB.CONTENT);
		time = getIntent().getStringExtra(NotesDB.TIME);
		path = getIntent().getStringExtra(NotesDB.PATH);*/
		entity = (Person2) getIntent().getSerializableExtra("entity");
		content = entity.getContent();
		time = entity.getCreatedAt();
		path = entity.getPath().getFileUrl(this);
		s_EditText = (EditText)findViewById(R.id.s_et);
		notesDB = new NotesDB(this);
		dbWriter = notesDB.getWritableDatabase();
		
		//如果没有图片就隐藏控件，否则就加载图片
		if (path.equals("null")) {
			gv_write_status.setVisibility(View.GONE);
		} else {
			gv_write_status.setVisibility(View.VISIBLE); 
			//ImageLoader.getInstance().displayImage(path, gv_write_status);
		}
		s_EditText.setText(content);
		s_EditText.requestFocus();
		
	}
	
	/**
	 * 处理Path字段  将String [uri1,uri2,uri3] 转换为 Arraylist<Uri>
	 */
	public void initPath() {
		//1.去除前后大括号
		//String uris = path.substring(1, path.length()-1);
		//去除空格
		//String uriss = uris.replace(" ", "");
		//将string转换为数组以逗号为分隔符
		//String[] uri = uriss.split(",");
		//将String数组转换为集合
		//List<String> list= java.util.Arrays.asList(uri);
		//遍历集合中的元素添加到ArrayList<Uri>中
		/*for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();*/
			Uri u = Uri.parse(path);
			imgUriss.add(u);
			statusImgsAdapter.notifyDataSetChanged();
		//}
	}
	
	
	//修改数据的方法
	public void modifydata() {
		ContentValues values = new ContentValues();
		String newcontent = s_EditText.getText().toString();
		values.put("content", newcontent);
		String[] args = {String.valueOf(id)};
		dbWriter.update(NotesDB.TABLE_NAME, values, "_id=?", args);
	}
	
	
	//初始化标题栏
	protected void initTitle() {
		new TitleBuilder(this).setLeftImage(R.drawable.navigationbar_back_highlighted)
		.setTitleText(time)
		.setRightImage(R.drawable.compose_emotion_delete_highlighted)
		.setLeftOnClickListener(this)
		.setRightOnClickListener(this);

	}

	//删除数据的方法 通过ID删除整条数据
	public void deleteData() {
		String[] args = {String.valueOf(id)};  
		dbWriter.delete(NotesDB.TABLE_NAME, "_id=?", args);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.titlebar_iv_left:
			modifydata();
			finish();
			break;

		case R.id.titlebar_iv_right:
			deleteData();
			finish();
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO 自动生成的方法存根
		//获取所点击ITEM的所应用的适配器
		Object itemAdapter = parent.getAdapter();
		//判断适配器的值 （九宫格图片）
		if(itemAdapter instanceof WriteStatusGridImgsAdapter) {
			if(position == statusImgsAdapter.getCount() - 1) {
				ImageUtils.showImagePickDialog(this);
			}

}
	}
	
}
